package com.itheima.controller;

import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SetMeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiNiuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Map;

/**
 * @author nglsk
 * @version v1.0
 * @date 2019/11/23 20:48
 * @description TODO
 **/

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Reference
    SetMealService setMealService;

    @Autowired
    JedisPool jedisPool;

    /**
     * 图片上传
     *
     * @param imgFile
     * @return
     * @throws IOException
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) throws IOException {

        //将图片名称转换成随机字符串
        String newFileName = toRandomImgName(imgFile);

        //调用七牛工具类上传图片
        String upload = QiNiuUtil.upload(imgFile.getBytes(), newFileName);

        if (!MessageConstant.PIC_UPLOAD_SUCCESS.equals(upload)) {
            return Result.error(upload);
        }

        Jedis resource = jedisPool.getResource();
        resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES, newFileName);
        resource.close();

        return Result.success(upload, newFileName);
    }

    /**
     * 将图片名称转换成随机字符串
     *
     * @param file
     * @return
     */
    private String toRandomImgName(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        return UUID.randomUUID().toString() + suffix;
    }

    /**
     * 套餐分页显示
     *
     * @param queryPageBean
     * @return
     */

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setMealService.findPage(queryPageBean);
    }


    /**
     * 添加套餐
     *
     * @param setMeal
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody SetMeal setMeal) {
        try {
            setMealService.add(setMeal);
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setMeal.getImg());
            resource.close();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.ADD_SETMEAL_FAIL);
        }
        return Result.success(MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 回显套餐数据
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById4Edit")
    public Result findById4Edit(Integer id) {
        try {
            Map map = setMealService.findById4Edit(id);
            return Result.success(MessageConstant.QUERY_SETMEAL_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }


    /**
     * 更新套餐信息
     *
     * @param setMeal
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody SetMeal setMeal) {
        try {
            setMealService.edit(setMeal);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return Result.success(MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 删除套餐信息
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Result delete(Integer id) {
        try {
            setMealService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return Result.success(MessageConstant.DELETE_SETMEAL_SUCCESS);
    }


}
